//
//  YXYSearchShowViewController.m
//  Topit
//
//  Created by dllo on 15/12/12.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYSearchShowViewController.h"
#import "YXYGroupSearchDataModel.h"
#import "YXYSearchShowCollectionViewCell.h"
#import "ShowBigPhoto.h"
#import "YXYShowViewController.h"
#import "MJRefresh.h"
@interface YXYSearchShowViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, dismissDelegate>
@property (nonatomic, copy) NSString *url;
@property (nonatomic, retain) NSMutableArray *searchData;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSMutableArray *imageArray;
@end

@implementation YXYSearchShowViewController
- (void)dealloc {
    [_imageArray release];
    [_myView release];
    [_collectionView release];
    [_name release];
    [_url release];
    [_searchData release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpCollectionView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.searchData = [NSMutableArray arrayWithCapacity:0];
    self.imageArray = [NSMutableArray arrayWithCapacity:0];
    self.number = 0;
    
    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    self.myView.backgroundColor = [UIColor colorWithHue:128 / 255.0 saturation:138 / 255.0 brightness:135 / 255.0 alpha:1];
    
    [self.view addSubview:self.myView];
    [_myView release];
    
    [self getData];
    [self setUpButton];
    
    [self getFootRefresh];
    // Do any additional setup after loading the view.
}
- (instancetype)initWithURL:(NSString *)url WithName:(NSString *)name
{
    self = [super init];
    if (self) {
        self.url = url;
        self.name = name;
    }
    return self;
}

- (void)getData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.mode = MBProgressHUDModeIndeterminate;
    [YXYNetWorkTool getWithUrl:self.url parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSNumber *num = result[@"info"][@"num"];
        if (num.intValue == 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
            label.textAlignment = 1;
            label.text = @"没有相关搜索";
            [self.view addSubview:label];
            [label release];
        }
        for (NSDictionary *dic in result[@"item"]) {
            YXYGroupSearchDataModel *searchDataModel = [[YXYGroupSearchDataModel alloc] initWithDictionary:dic];
            [self.searchData addObject:searchDataModel];
            [self.imageArray addObject:searchDataModel.icon[@"url_l"]];
            [searchDataModel release];
        }
        [hud hide:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    } fail:^(NSError *error) {
        [hud hide:YES];
        [ProgressHUD showError:@"请检查网络"];
    }];
}

#pragma mark - 底部刷新
//- (void)getFootRefresh {
//    [self.collectionView addFooterWithCallback:^{
//        self.number += 30;
//        NSString *num = [NSString stringWithFormat:@"%d", _number];
//        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:num, @"offset", nil];
//        [YXYNetWorkTool getWithUrl:self.url parameter:dic httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
//            self.num = result[@"info"];
//            for (NSDictionary *dic in result[@"item"]) {
//                YXYGroupSearchDataModel *searchDataModel = [[YXYGroupSearchDataModel alloc] initWithDictionary:dic];
//                [self.searchData addObject:searchDataModel];
//                [self.imageArray addObject:searchDataModel.icon[@"url_l"]];
//                [searchDataModel release];
//            }
//
//                [_collectionView reloadData];
//
//                [_collectionView footerEndRefreshing];
//
//        } fail:^(NSError *error) {
//
//        }];
//
//    }];
//
//}
-(void)getFootRefresh {
    [self.collectionView addFooterWithCallback:^{
        self.number += 30;
        NSString *num = [NSString stringWithFormat:@"%d", _number];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:num, @"offset", nil];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"正在加载";
        hud.mode = MBProgressHUDModeIndeterminate;
        [YXYNetWorkTool getWithUrl:self.url parameter:dic httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
            if ([result[@"item"] isKindOfClass:[NSArray class]]) {
                self.num = result[@"info"];
                for (NSDictionary *dic in result[@"item"]) {
                    YXYGroupSearchDataModel *searchDataModel = [[YXYGroupSearchDataModel alloc] initWithDictionary:dic];
                    [self.searchData addObject:searchDataModel];
                    [self.imageArray addObject:searchDataModel.icon[@"url_l"]];
                    [searchDataModel release];
                }
                [hud hide:YES];
            }else {
                UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"亲爱的" message:@"已经到底er了" preferredStyle:UIAlertControllerStyleAlert];
                [alertView addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                [self presentViewController:alertView animated:YES completion:nil];
            }
            [hud hide:YES];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_collectionView reloadData];
                [_collectionView footerEndRefreshing];
            });
        } fail:^(NSError *error) {
            [hud hide:YES];
            [ProgressHUD showError:@"请检查网络"];
        }];
    }];
    
}


#pragma mark - button
- (void) setUpButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(5, 5, 35, 35);
    [button setImage:[[UIImage imageNamed:@"searchBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backDidPress:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(button.width - 10, 0, self.view.width - 35, self.myView.height)];
    label.text = self.name;
    label.textAlignment = 1;
    [self.myView addSubview:label];
    [label release];
    [self.myView addSubview:button];
}

- (void)backDidPress:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - collectionView
- (void)setUpCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.width / 3, self.view.width / 3);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[YXYSearchShowCollectionViewCell class] forCellWithReuseIdentifier:@"YXYSearchShowCollectionViewCellIdentifier"];
    [self.view addSubview:self.collectionView];
    [flowLayout release];
    [_collectionView release];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.searchData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYSearchShowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYSearchShowCollectionViewCellIdentifier" forIndexPath:indexPath];
    YXYGroupSearchDataModel *groupSearchDataModel = self.searchData[indexPath.item];
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:groupSearchDataModel.icon[@"url"]]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYShowViewController *showVC = [[YXYShowViewController alloc] init];
    showVC.view.backgroundColor = [UIColor redColor];
    ShowBigPhoto *showBigPhoto = [[ShowBigPhoto alloc] initWithFrame:showVC.view.bounds withArray:self.imageArray withnum:indexPath.item withStepArray:nil];
    [showVC.view addSubview:showBigPhoto];
    showBigPhoto.delegate = self;
    [self presentViewController:showVC animated:YES completion:^{
        
    }];
    [showVC release];
}

-(void)dismiss {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
